package com.dog.basics.service.mtkj.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.dog.basics.bean.dto.admin.mtkj.GoodTypeAddDto;
import com.dog.basics.bean.dto.admin.mtkj.GoodTypeEditDto;
import com.dog.basics.bean.model.common.DogUser;
import com.dog.basics.bean.model.mtkj.MtGoods;
import com.dog.basics.bean.model.mtkj.MtGoodsType;
import com.dog.basics.bean.model.mtkj.MtShopGoodType;
import com.dog.basics.bean.vo.admin.mtkj.GoodTypeFromVo;
import com.dog.basics.bean.vo.api.mtkj.ApiGoodsTypeVo;
import com.dog.basics.common.util.PageParam;
import com.dog.basics.dao.common.DogUserMapper;
import com.dog.basics.dao.mtkj.MtGoodsMapper;
import com.dog.basics.dao.mtkj.MtGoodsTypeMapper;
import com.dog.basics.dao.mtkj.MtShopGoodTypeMapper;
import com.dog.basics.security.util.SecurityUtils;
import com.dog.basics.service.mtkj.IMtGoodsTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 商品类型表 服务实现类
 * </p>
 *
 * @author wph
 * @since 2020-11-09
 */
@Service
@AllArgsConstructor
public class MtGoodsTypeServiceImpl extends ServiceImpl<MtGoodsTypeMapper, MtGoodsType> implements IMtGoodsTypeService {

    private final MtGoodsMapper goodsMapper;
    private final DogUserMapper userMapper;
    private final MtShopGoodTypeMapper shopGoodTypeMapper;

    /**
     * Admin服务
     *  查询类别并进行分页
     * @param page
     * @return
     */
    @Override
    public IPage<GoodTypeFromVo> adminFindGoodType(PageParam page) {

        IPage iPage = this.page(page,null);
        List<MtGoodsType> goodsTypeList = iPage.getRecords();
        List<GoodTypeFromVo> list = goodsTypeList.stream().map(e -> {

            GoodTypeFromVo fromVo = BeanUtil.copyProperties(e, GoodTypeFromVo.class);

            DogUser crtUser = userMapper.selectById(e.getCreateBy());
            fromVo.setCreateBy(ObjectUtil.isNotNull(crtUser) ? crtUser.getNickName() : "");

            DogUser updUer = userMapper.selectById(e.getUpdateBy());
            fromVo.setUpdateBy(ObjectUtil.isNotNull(updUer) ? updUer.getNickName() : "");

            return fromVo;
        }).collect(Collectors.toList());
        iPage.setRecords(list);
        return iPage;
    }

    /**
     * 查询所有类型列表
     *
     * @return
     * @author hangdy
     */
    @Override
    public IPage<ApiGoodsTypeVo> findGoodTypes(PageParam page) {
        IPage iPage = this.page(page,null);
        List<MtGoodsType> goodsTypeList = iPage.getRecords();
        List<ApiGoodsTypeVo> list = goodsTypeList.stream().map(e -> BeanUtil.copyProperties(e, ApiGoodsTypeVo.class)).collect(Collectors.toList());
        iPage.setRecords(list);
        return iPage;
    }

    /**
     * Admin服务
     *  新增商品类别
     *
     * @param goodTypeAddDot
     * @return
     * @author hangdy
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveGoodType(GoodTypeAddDto goodTypeAddDot) {

        MtGoodsType goodsType = new MtGoodsType();
        BeanUtil.copyProperties(goodTypeAddDot, goodsType);

        goodsType.setCreateTime(DateUtil.currentSeconds());
        goodsType.setCreateBy(SecurityUtils.getSysUser().getUserId());
        //2.保存商品类别
        Validator.validateTrue(this.save(goodsType), "新增商品类别失败");
        return Boolean.TRUE;
    }

    /**
     * Admin服务
     *  修改商品类型
     *
     * @param goodTypeEditDto
     * @return
     * @author hangdy
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean editGoodType(GoodTypeEditDto goodTypeEditDto) {

        //2.查询商品类别是否存在
        MtGoodsType goodsType = this.getById(goodTypeEditDto.getId());
        Validator.validateNotNull(goodsType, "未找到该商品类别");

        BeanUtil.copyProperties(goodTypeEditDto, goodsType);
        goodsType.setUpdateTime(DateUtil.currentSeconds());
        goodsType.setUpdateBy(SecurityUtils.getSysUser().getUserId());
        //3.跟新商品类别
        Validator.validateTrue(this.updateById(goodsType), "修改商品类别失败");
        return Boolean.TRUE;
    }

    /**
     * Admin服务
     *  删除商品类型
     *
     * @param id
     * @return
     * @author hangdy
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteGoodType(Long id) {

        //2.判断商品类型是否存在
        MtGoodsType goodsType = this.getById(id);
        Validator.validateNotNull(goodsType, "无此数据");
        //3.判断该类别下是否有商铺商品类别
        List<MtShopGoodType> shopGoodTypeList = shopGoodTypeMapper.selectList(Wrappers.<MtShopGoodType>lambdaQuery().eq(MtShopGoodType::getTypeId,id));
        if(shopGoodTypeList.size() != 0){
            throw new RuntimeException("该类别下有店铺商品类别无法删除");
        }
        //4.判断该类别下是否存在商品
        List<MtGoods> goodsList = goodsMapper.selectList(Wrappers.<MtGoods>lambdaQuery().eq(MtGoods::getTypeId, id));
        if(goodsList.size() != 0){
            throw new RuntimeException("该类别下有商品无法删除");
        }
        //5.删除操作
        Validator.validateTrue(this.removeById(id), "删除商品类别失败");
        return Boolean.TRUE;
    }
}
